package hlhj.fhp.supreme.activitys

import android.annotation.SuppressLint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.Contents
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.activity_change_pwd_aty.*

class ChangePwdAty : BaseAty() {
    private var sec=60
    private val  handler= @SuppressLint("HandlerLeak")
    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (sec>0){
                sec--
                btSendSMS.text="$sec s"
                this.sendEmptyMessageDelayed(1,1000)
            }else{
                sec=60
                btSendSMS.text="验证码"
                btSendSMS.isClickable=true
            }
        }
    }
    override fun getContentId(): Int {
        return R.layout.activity_change_pwd_aty
    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btSendSMS.setOnClickListener {
            if (etPhone.text.isEmpty()){
                MyUtils.toast("手机号不能为空")
                return@setOnClickListener
            }
            handler.sendEmptyMessage(1)
        }


        btCommit.setOnClickListener {
            if (etPhone.text.isEmpty()){
                MyUtils.toast("手机号不能为空")
                return@setOnClickListener
            }
            if (etYzm.text.isEmpty()){
                MyUtils.toast("验证码不能为空")
                return@setOnClickListener
            }
            if (et_pwd.text.isEmpty()){
                MyUtils.toast("密码不能为空")
                return@setOnClickListener
            }
            if (et_pwd2.text.isEmpty()){
                MyUtils.toast("重复密码不能为空")
                return@setOnClickListener
            }
            if (!et_pwd.text.toString().equals(et_pwd2.text.toString())){
                MyUtils.toast("两次输入的密码不一致")
                return@setOnClickListener
            }

            OkGo.post<BaseBean>(Urls.BaseUrl+Urls.CHANGE_PWD)
                    .params("mobile",etPhone.text.toString())
                    .params("captcha",etYzm.text.toString())
                    .params("user_pass",et_pwd.text.toString())
                    .execute(object:JsonCallBack<BaseBean>(this){
                        override fun onSuccess(response: Response<BaseBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                MyUtils.toast("修改密码成功")
                                SPHelper.saveLoginStatus(this@ChangePwdAty,true)
                                SPHelper.saveUserInfo(this@ChangePwdAty,etPhone.text.toString(),et_pwd.text.toString())
                                finish()
                            }else{
                                MyUtils.toast("修改密码失败")
                                finish()
                            }
                        }
                    })
        }
    }

}
